package com.miplus.ccrm.common;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class IPLogConfig extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent event) {
        RequestAttributes requestAttributes= RequestContextHolder.getRequestAttributes();
        if(requestAttributes==null){
            return "127.0.0.1";
        }
        HttpServletRequest request=((ServletRequestAttributes)requestAttributes).getRequest();
        return JPubUtils.getReqIpAddr(request);
    }
}